package com.px.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.px.dto.ReturnResult;
import com.px.pojo.ShUser;
import com.px.service.UserService;
import com.px.utils.EmptyUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private ObjectMapper objectMapper;

    /**
     * 注册
     * @param shUser
     * @return
     * @throws Exception
     */
    @GetMapping("/a")
    public ReturnResult zc(ShUser shUser)throws Exception{
        return userService.login(shUser);
    }

    /**
     * 登录
     * @param account
     * @param password
     * @return
     * @throws Exception
     */
    @PostMapping("/b")
    public String dl(String account,String password)throws Exception{
        ReturnResult denglu = userService.denglu(account, password);
        if (EmptyUtils.isNotEmpty(denglu)){
            return "home";
        }
        return  "index";
    }

    /**
     * 退出
     * @return
     * @throws Exception
     */
    @PostMapping("/c")
    public String tc(String token)throws Exception{
        ReturnResult returnResult = userService.deleteToken(token);
        if (returnResult != null){
            return "index";
        }
        return null;
    }



    /**
     * 微信登录
     * @throws Exception
     *//*
    @GetMapping("/api/users/login/wechat")
    public void weChatLogin(HttpServletResponse response) throws Exception{
        String url = "https://open.weixin.qq.com/connect/qrconnect?appid=wxbdc5610cc59c1631&redirect_uri=https%3A%2F%2Fpassport.yhd.com%2Fwechat%2Fcallback.do&response_type=code&scope=snsapi_login&state=3d6be0a4035d839573b04816624a415e#wechat_redirect";
        try {
            StringBuffer openBuilder = new StringBuffer(wechatConfig.getOpen_url())
                    .append("?").append("appid=").append(wechatConfig.getAppid())
                    .append("&").append("redirect_uri=").append(wechatConfig.getRedirect_uri())
                    .append("&").append("response_type=").append("code")
                    .append("&").append("scope=").append("snsapi_login")
                    .append("&").append("state=").append(wechatConfig.getState())
                    .append("#wechat_redirect");
            log.info("#####构建的URL:{}",openBuilder.toString());
            response.sendRedirect(openBuilder.toString());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("唤起微信登录页面失败:{}", e);
        }
        response.sendRedirect(url);
    }
*/
   /* @PostMapping("/api/users/wechat/callback")
    public void weChatCallBack(@RequestParam String code,
                               HttpServletRequest request,
                               HttpServletResponse response) throws Exception{
        try {
            StringBuffer openBuilder = new StringBuffer(wechatConfig.getAccess_token_url())
                    .append("?").append("appid=").append(wechatConfig.getAppid())
                    .append("&").append("secret=").append(wechatConfig.getSecret())
                    .append("&").append("code=").append(code)
                    .append("&").append("grant_type=").append("authorization_code");
            log.info("#####构建的URL:{}",openBuilder.toString());
            //通过code换取ACCESS_TOKEN
            String json = UrlUtils.loadURL(openBuilder.toString());
            //将获取的json数据进行反序列化
            Map<String,Object> wechatToken = objectMapper.readValue(json, Map.class);
            //验证本地仓储是否存在该用户
            String openid = wechatToken.get("openid").toString();
            String accessToken = wechatToken.get("access_token").toString();
            UsersDto usersDto = userService.loginByWechat(accessToken, openid);
            String token = usersDto.getToken();
            //返回前端处理
            String loginPage = "http://localhost/index.html?token="+token;
            response.sendRedirect(loginPage);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("重定向失败:{}", e);
        }
    }
*/
}
